//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class ManufacturerDBManager
    {
        #region Methods
        public static ManufacturerCollection GetAllManufacturers(bool showHidden)
        {
            ManufacturerCollection manufacturerCollection = new ManufacturerCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ManufacturerLoadAll");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Manufacturer manufacturer = new Manufacturer();
                    manufacturer.ManufacturerID = NopDataHelper.GetInt(dataReader, "ManufacturerID");
                    manufacturer.Name = NopDataHelper.GetString(dataReader, "Name");
                    manufacturer.Description = NopDataHelper.GetString(dataReader, "Description");
                    manufacturer.TemplateID = NopDataHelper.GetInt(dataReader, "TemplateID");
                    manufacturer.MetaKeywords = NopDataHelper.GetString(dataReader, "MetaKeywords");
                    manufacturer.MetaDescription = NopDataHelper.GetString(dataReader, "MetaDescription");
                    manufacturer.MetaTitle = NopDataHelper.GetString(dataReader, "MetaTitle");
                    manufacturer.SEName = NopDataHelper.GetString(dataReader, "SEName");
                    manufacturer.PictureID = NopDataHelper.GetInt(dataReader, "PictureID");
                    manufacturer.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    manufacturer.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    manufacturer.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    manufacturer.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    manufacturer.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                    manufacturerCollection.Add(manufacturer);
                }
            }

            return manufacturerCollection;
        }

        public static Manufacturer GetByManufacturerID(int ManufacturerID)
        {

            Manufacturer manufacturer = null;
            if (ManufacturerID == 0)
                return manufacturer;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ManufacturerLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "ManufacturerID", DbType.Int32, ManufacturerID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    manufacturer = new Manufacturer();
                    manufacturer.ManufacturerID = NopDataHelper.GetInt(dataReader, "ManufacturerID");
                    manufacturer.Name = NopDataHelper.GetString(dataReader, "Name");
                    manufacturer.Description = NopDataHelper.GetString(dataReader, "Description");
                    manufacturer.TemplateID = NopDataHelper.GetInt(dataReader, "TemplateID");
                    manufacturer.MetaKeywords = NopDataHelper.GetString(dataReader, "MetaKeywords");
                    manufacturer.MetaDescription = NopDataHelper.GetString(dataReader, "MetaDescription");
                    manufacturer.MetaTitle = NopDataHelper.GetString(dataReader, "MetaTitle");
                    manufacturer.SEName = NopDataHelper.GetString(dataReader, "SEName");
                    manufacturer.PictureID = NopDataHelper.GetInt(dataReader, "PictureID");
                    manufacturer.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    manufacturer.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    manufacturer.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    manufacturer.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    manufacturer.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                }
            }
            return manufacturer;
        }

        public static Manufacturer InsertManufacturer(string Name, string Description,
            int TemplateID, string MetaKeywords, string MetaDescription, string MetaTitle,
            string SEName, int PictureID, bool Published, bool Deleted,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Manufacturer manufacturer = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ManufacturerInsert");
            db.AddOutParameter(dbCommand, "ManufacturerID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "Description", DbType.String, Description);
            db.AddInParameter(dbCommand, "TemplateID", DbType.Int32, TemplateID);
            db.AddInParameter(dbCommand, "MetaKeywords", DbType.String, MetaKeywords);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, MetaDescription);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, MetaTitle);
            db.AddInParameter(dbCommand, "SEName", DbType.String, SEName);
            db.AddInParameter(dbCommand, "PictureID", DbType.Int32, PictureID);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int ManufacturerID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@ManufacturerID"));
                manufacturer = GetByManufacturerID(ManufacturerID);
            }
            return manufacturer;
        }

        public static Manufacturer UpdateManufacturer(int ManufacturerID, string Name, string Description,
            int TemplateID, string MetaKeywords, string MetaDescription, string MetaTitle,
            string SEName, int PictureID, bool Published, bool Deleted,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Manufacturer manufacturer = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ManufacturerUpdate");
            db.AddInParameter(dbCommand, "ManufacturerID", DbType.Int32, ManufacturerID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "Description", DbType.String, Description);
            db.AddInParameter(dbCommand, "TemplateID", DbType.Int32, TemplateID);
            db.AddInParameter(dbCommand, "MetaKeywords", DbType.String, MetaKeywords);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, MetaDescription);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, MetaTitle);
            db.AddInParameter(dbCommand, "SEName", DbType.String, SEName);
            db.AddInParameter(dbCommand, "PictureID", DbType.Int32, PictureID);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                manufacturer = GetByManufacturerID(ManufacturerID);


            return manufacturer;
        }
        #endregion
    }
}
